﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Text.RegularExpressions;

namespace AppBrowser
{
    /// <summary>
    /// Individual Application data
    /// </summary>
    public class AppItem
    {
        public string AppName;
        public string App_Id;
        public string AppUrl;
        public string Description;
        public string Category_Id;
        public string Category;
        public string Publisher;
        public string PublisherUrl;
        public string PublisherDesc;
        public string License;
        public string SystemRequirements;
        
        // Current Version selected for this item
        public int SelectedVersionIndex;

        public List<AppVersion> VersionList = new List<AppVersion>();

        public AppItem()
        {
        }

        public AppItem(string app_id, string appname)
        {
            App_Id = app_id;
            AppName = appname;
          
        }

        /// <summary>
        /// Fill details from a datarow
        /// </summary>
        /// <param name="dr"></param>
        public AppItem(System.Data.DataRow dr)
        {
            App_Id = dr["App_Id"].ToString();
            AppUrl = dr["appurl"].ToString();
            AppName = dr["name"].ToString();
            Publisher = dr["publisher"].ToString();
            PublisherUrl = dr["publisherUrl"].ToString();
            Category_Id = dr["Category_Id"].ToString();
            License = dr["license"].ToString();
            Description = dr["Description"].ToString();

        }

        /// <summary>
        /// Get selected version
        /// </summary>
        public AppVersion GetSelectedVersion()
        {
            if (VersionList.Count > this.SelectedVersionIndex)
            {
                return this.VersionList[SelectedVersionIndex];
            }
            return null;
        }

        /// <summary>
        /// Get latest release version
        /// </summary>
        /// <returns></returns>
        public AppVersion GetLatestVersion()
        {
            if (VersionList.Count > 0)
            {
                return this.VersionList[0];
            }
            return null;
        }


        /// <summary>
        /// Add datarows of versions
        /// </summary>
        /// <param name="drs"></param>
        public void AddVersions(System.Data.DataRow[] drs)
        {
            if (drs != null)
            {
                foreach (DataRow dr in drs)
                {
                    this.VersionList.Add(new AppVersion(dr));
                }
            }
        }

        public AppVersion GetVersion(int index)
        {
            if (VersionList.Count > index)
            {
                return this.VersionList[index];
            }
            else
            {
                AppVersion a = new AppVersion("0.0", "NOURL");
                return a;
            }
        }

        public string GetVersionNumber(int index)
        {
            if (this.VersionList.Count > index)
            {
                return this.VersionList[index].VersionNumber;
            }

            return "";
        }

        /// <summary>
        /// Get valid filename
        /// </summary>
        /// <returns></returns>
        public string GetFileName()
        {
            string tFile = Regex.Replace(AppName, @"[?:\/*""<>|]", "");
            return tFile;
        }


        /// <summary>
        /// Build filepath from string
        /// </summary>
        /// <returns></returns>
        /*
        public string BuildFilePath(string FilePathString)
        {
            string tmpStr;
            string tFile = Regex.Replace(AppFileName, @"[?:\/*""<>|]", "");
            tmpStr = FilePathString.Replace("[Publisher]", Publisher);
            tmpStr = tmpStr.Replace("[AppName]", AppName);
            tmpStr = tmpStr.Replace("[AppVersion]", AppVersion);
            tmpStr = tmpStr.Replace("[AppFile]", tFile);
            return tmpStr;
        }
         */


    }

    public class AppVersion
    {
        public string VersionHistory_Id;
        public string AppVersion_Id;
        public string VersionNumber;
        public string DownloadUrl;
        public string VersionType;
        public string VersionDescription;
        public string VersionInfoUrl;
        public string ReleaseDate;
        public string FileName;

        public AppVersion()  { }

        public AppVersion(string versionnumber, string downloadurl)
        {
            VersionNumber = versionnumber;
            DownloadUrl = downloadurl;
        }

        public AppVersion(System.Data.DataRow dr)
        {
            VersionHistory_Id = dr["VersionHistory_Id"].ToString();
            VersionNumber = dr["version"].ToString();
            VersionDescription = dr["VersionDescription"].ToString();
            VersionType = dr["versionType"].ToString();
            DownloadUrl = dr["VersionDownloadUrl"].ToString();
            FileName = dr["filename"].ToString();
        }
        public override string ToString()
        {
            return VersionNumber;
        }

        /// <summary>
        /// Attempt to get filename from url
        /// ie "http://www.thejackol.com/files/project.exe"
        /// returns project.ext
        /// </summary>
        /// <returns></returns>
        public string GetFileNameFromUrl()
        {
            string URL = DownloadUrl;

            /*
            string FileName = URL.Substring(URL.LastIndexOf("/") + 1,
                (URL.Length - URL.LastIndexOf("/") - 1));
            */

            string FileName = System.IO.Path.GetFileName(URL);
            int index = FileName.IndexOf("?");

            if (index > -1)
            {
                FileName = FileName.Remove(index);
            }
            
            // Regex.Replace(FileName, @"[?:\/*""<>|]", "");
            return FileName;
        }

    }
}
